const addWorkSubjectUrl = require('../../../../../config').addWorkSubject // 增加作业科目
const addCompleteSubjectUrl = require('../../../../../config').addCompleteSubject // 增加科目完成
const getImgUrl = require('../../../../../config').getImgUrl  // 获取图片Url 
const getStudentHomeworkByIdUrl = require('../../../../../config').getStudentHomeworkByIdUrl // 获取某个学生的作业
var util = require('../../../../../utils/util.js')
let parms = {
  completeByStudentImgUrl :[],
  evaluateByTeacherContent : '',
  homeworkScoreComplete : 80,
  homeworkScoreModify : 80,
  modifyByTeacherImgUrl : []
}
let score = []
for(let i =40;i<101;i++){
  score.push(i)
}
Page({
	data: {
		files: [], // 展示上传的照片
    preScoreArray: score, // 分数
    preIndex: 40,
    afterScoreArray: score,
    afterIndex: 40,
		ismodel: true, // 是否要输入框
		isscroll: true,
		ismodelafter: true,
		inpTextpro: '还有需要特别说明的吗？', // 要什么要说的咩
		classListArray:[
			{
				name:'语文',
				id: 20
			},{
				name:'数学',
				id: 2
			},{
				name:'英语',
				id: 12
			}
		],
		index: 0,
    staffinfo: null, // 接受上一个页面传下来的用户信息
    status: null // 照片上传状态。辅导前和辅导后
	},
	onLoad: function(option) {
    console.log(option.staffinfo)
    console.log(score)
    this.setData({
      staffinfo: JSON.parse(option.staffinfo)
    })
		let that = this
		let time = util.formatTime(new Date());
		let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
		let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
		this.setData({
			scroll_height: windowHeight * 2,
			time: time
		})
	},
  // api 增、添加科目 2.查、获取作业ID 3.增、作业完成 4.通用、获取照片URL
  apiAddSubject(parmas){
    wx.request({
      url: addWorkSubjectUrl,
      data: parmas,
      method: 'POST',
      success: (res) => {
        console.log(res)
        parms.id = res.data.data
        if(res.data.status == 200){
          wx.showToast({
            title: '添加科目成功',
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      },
      fail: (res) => {
        console.log(res)
        wx.showModal({
          title: '提示',
          content: res.data.msg,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },
  apiAddWork(parms){
    wx.request({
      url: addCompleteSubjectUrl,
      data: parms,
      method: 'POST',
      success: (res) => {
        console.log(res)
        if(res.data.status == 200){
          wx.showToast({
            title: '添加成功,返回上一页',
            icon: 'success',
            duration: 2000
          })
          wx.navigateBack({
            
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })          
        }
      }
    })
  },
  apiGetUrlByimg(parmas){
    let that = this
    wx.uploadFile({
      url: getImgUrl,
      filePath: parmas,
      name: 'file',
      method: 'POST',
      formData: {
        organizationId:'1'
      },
      success(res) {
        let imgUrl = JSON.parse(res.data).data[0]
        console.log(imgUrl)
        if(that.data.status === '辅导前'){
          parms.completeByStudentImgUrl.push(imgUrl)
        }else if(that.data.status === '辅导后'){
          parms.modifyByTeacherImgUrl.push(imgUrl)
        }
        wx.showToast({
          title: '上传成功',
          icon: 'success',
          duration: 2000,
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '上传失败',
          icon: 'success',
          duration: 2000,
        })
      }
    })
  },
  // api END
	bindPickerChange(e) {
    let that = this
    let staffInfo = this.data.staffinfo
    let index = e.detail.value
    this.setData({
      index: e.detail.value
    })
    console.log(this.data.classListArray[index].name)
    wx.getStorage({
      key: 'teacherInfo',
      success: function (res) {
        console.log(res)
        let parms = {
          "createUserId": res.data.staffId,
          "homeworkId": staffInfo.id,
          "homeworkType": this.data.classListArray[index].name,
          "studentName": staffInfo.studentName
        }
        that.apiAddSubject(parms)
      },
    })
	},
  // 获取分数 Start
  proGetScore(e){
    console.log(this.data.preScoreArray[e.detail.value])
    parms.homeworkScoreComplete = this.data.preScoreArray[e.detail.value]
    this.setData({
      preIndex: e.detail.value
    })

  },
  agerGetScore(e){
    console.log(this.data.afterScoreArray[e.detail.value])
    parms.homeworkScoreModify = this.data.afterScoreArray[e.detail.value]
    this.setData({
      afterIndex: e.detail.value
    })
  },
  // 获取分数 END
  // 获取图片的URL
	chooseImage: function(e) {
		var that = this;
    console.log(e.currentTarget.dataset.status)
    let status = e.currentTarget.dataset.status
    this.setData({
      status : status
    })
		wx.chooseImage({
			count: 4,
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function(res) {
        console.log(res.tempFilePaths[0])
        that.apiGetUrlByimg(res.tempFilePaths[0])
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        if(status == '辅导前'){
          that.setData({
            filesPor: that.data.files.concat(res.tempFilePaths)
          })
        }else if(status == '辅导后'){
          that.setData({
            filesAfter: that.data.files.concat(res.tempFilePaths)
          })          
        }
			}
		})
	},
	previewImage: function(e) {
		wx.previewImage({
			current: e.currentTarget.id, // 当前显示图片的http链接
			urls: this.data.files // 需要预览的图片http链接列表
		})
    console.log(e)
	},
	// 获取评价 start
	inpTextpro: function(e) {
    this.setData({
			inpTextpro: e.detail.value
		})
    console.log(this.data.inpTextpro)
	},
	inpTextafter: function(e) {
    this.setData({
			inpTextafter: e.detail.value
		})
    console.log(this.data.inpTextafter)
	},
  // 获取评价 END

	// 弹出输入框
	popModel: function() {
		let that = this;
		that.setData({
			ismodel: !that.data.ismodel,
			isscroll: !that.data.isscroll
		})
	},
	popModelafter: function() {
    let that = this
    this.setData({
			ismodelafter: !that.data.ismodel,
			isscroll: !that.data.isscroll
		})
	},
	// 隐藏输入框
	hiddentModel: function() {
		let that = this
		that.setData({
			ismodel: !that.data.ismodel,
			isscroll: !that.data.isscroll
		})
	},
	hiddentModelafter: function() {
		let that = this
		that.setData({
			ismodelafter: !that.data.ismodelafter,
			isscroll: !that.data.isscroll
		})
	},
	// 确认输入
	handleText: function() {
		let that = this
		that.setData({
			inpTextarea: that.data.inpTextpro,
			ismodel: !that.data.ismodel,
			isscroll: !that.data.isscroll
		})
	},
	handleTextafter: function() {
		let that = this
		that.setData({
			inpTextafter: that.data.inpTextafter,
			ismodelafter: !that.data.ismodelafter,
			isscroll: !that.data.isscroll
		})
	},
  completeWork(e){
    parms.evaluateByTeacherContent = this.data.inpTextpro 
    parms.evaluateByTeacherContentAfter = this.data.inpTextafter
    wx.getStorage({
      key: 'teacherInfo',
      success: (res) => {
        console.log(parms)
        this.apiAddWork(parms)
      }
    })
  }
})
